package kr.or.smardi.smardi_AScan.Scan;

import kr.or.smardi.smardi_AScan.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class AC_Warning extends Activity {

	LinearLayout layout_warning = null;
	ImageView image_warning = null;
	/**
	 * Epi가 꽂힌 상태를 나타내기 위한 변수 headSetState 0: 뽑혔을 때 1: 꽂혀있을 때
	 */
	int headSetState = 0;
	int hasMicrophone = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_warning);
		
		layout_warning = (LinearLayout)findViewById(R.id.layout_warning);
		image_warning = (ImageView)findViewById(R.id.image_warning);
		if(getString(R.string.language).equals("en")) {
			image_warning.setImageDrawable(getResources().getDrawable(R.drawable.warning_front_en));
		}
		
		layout_warning.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
				startActivity(new Intent(AC_Warning.this, AC_Scan_process.class));
			}
		});
		
		cTimer.start();
		
		// BraodcastReceiver
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_HEADSET_PLUG);
		registerReceiver(mBroadcastReceiver, filter);
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		cTimer.cancel();
		unregisterReceiver(mBroadcastReceiver);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:

			gotoMeasure();
			return false;
		}

		return super.onKeyDown(keyCode, event);
	}
	
	private void gotoMeasure() {
		Intent intent = new Intent(AC_Warning.this, AC_Scan_main.class);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.fadein, R.anim.fadeout);
	}

	CountDownTimer cTimer = new CountDownTimer(7000, 1000) {
		
		@Override
		public void onTick(long millisUntilFinished) {
			//Do nothing
		}
		
		@Override
		public void onFinish() {
			finish();
			startActivity(new Intent(AC_Warning.this, AC_Scan_process.class));
		}
	};
	
	
	// -------------------------------------------------------
	private void checkConnection() {
		// AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
		// Log.i("smardi.Epi", "Wire connection:" + am.isWiredHeadsetOn());

		if (headSetState == 1 && hasMicrophone == 1) {

		} else {
			finish();
			startActivity(new Intent(AC_Warning.this, AC_Scan_main.class));
		}
	}
	
	
	BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
				/**
				 * headSetState 0: 뽑혔을 때 1: 꽂혀있을 때
				 */

				headSetState = intent.getIntExtra("state", 0);
				hasMicrophone = intent.getIntExtra("microphone", 0);
				// Log.e("Epi", "MIC:" + hasMicrophone);
				checkConnection();

			}
		}
	};
}
